package cn.wit.baoyan.controller;

import cn.wit.baoyan.condition.BaoyanCondition;
import cn.wit.baoyan.condition.BaoyanInsertCondition;
import cn.wit.baoyan.condition.BaoyanUpdateCondition;
import cn.wit.utils.pojo.Baoyan;
import cn.wit.baoyan.service.BaoyanService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/baoyan")
@CrossOrigin
@Api(tags = "保研管理")
public class BaoyanController {
    @Autowired
    private BaoyanService baoyanService;

    @PostMapping("/insertBaoyan")
    @ApiOperation("新增保研信息")
    public Result insertBaoyan(@RequestBody BaoyanInsertCondition condition) throws ParseException {
        Baoyan baoyan = new Baoyan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(condition.getBeginTime() != null && !condition.getBeginTime().equals("")) {
            Date beginTime = sdf.parse(condition.getBeginTime());
            baoyan.setBeginTime(beginTime);
        }
        if(condition.getEndTime() != null && !condition.getEndTime().equals("")) {
            Date endTime = sdf.parse(condition.getEndTime());
            baoyan.setEndTime(endTime);
        }
        if(condition.getEntryBeginTime() != null && !condition.getEntryBeginTime().equals("")) {
            Date entryBeginTime = sdf.parse(condition.getEntryBeginTime());
            baoyan.setEntryBeginTime(entryBeginTime);
        }
        if(condition.getEntryEndTime() != null && !condition.getEntryEndTime().equals("")) {
            Date entryEndTime = sdf.parse(condition.getEntryEndTime());
            baoyan.setEntryEndTime(entryEndTime);
        }
        baoyan.setCategory(condition.getCategory());
        baoyan.setInstitute(condition.getInstitute());
        baoyan.setIntroduce(condition.getIntroduce());
        baoyan.setSchool(condition.getSchool());
        baoyan.setLink(condition.getLink());
        return baoyanService.insertBaoyan(baoyan);
    }

    @PostMapping("/updateBaoyan")
    @ApiOperation("更新保研信息")
    public Result updateBaoyan(@RequestBody BaoyanUpdateCondition condition) throws ParseException {
        Baoyan baoyan = new Baoyan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(condition.getBeginTime() != null || !condition.getBeginTime().equals("")) {
            Date beginTime = sdf.parse(condition.getBeginTime());
            baoyan.setBeginTime(beginTime);
        }
        if(condition.getEndTime() != null || !condition.getEndTime().equals("")) {
            Date endTime = sdf.parse(condition.getEndTime());
            baoyan.setEndTime(endTime);
        }
        if(condition.getEntryBeginTime() != null || !condition.getEntryBeginTime().equals("")) {
            Date entryBeginTime = sdf.parse(condition.getEntryBeginTime());
            baoyan.setEntryBeginTime(entryBeginTime);
        }
        if(condition.getEntryEndTime() != null || !condition.getEntryEndTime().equals("")) {
            Date entryEndTime = sdf.parse(condition.getEntryEndTime());
            baoyan.setEntryEndTime(entryEndTime);
        }
        baoyan.setId(condition.getId());
        baoyan.setCategory(condition.getCategory());
        baoyan.setInstitute(condition.getInstitute());
        baoyan.setIntroduce(condition.getIntroduce());
        baoyan.setSchool(condition.getSchool());
        baoyan.setLink(condition.getLink());
        return baoyanService.updateBaoyan(baoyan);
    }

    @PostMapping("/deleteBaoyan/{id}")
    @ApiOperation("删除保研信息")
    public Result deleteBaoyan(@PathVariable("id") Integer id) {
        return baoyanService.deleteBaoyan(id);
    }

    @PostMapping("/selectById")
    @ApiOperation("根据id查询保研信息")
    public Result selectById(Integer id) {
        return baoyanService.selectById(id);
    }


    @GetMapping("/searchBaoyan/{num}/{size}")
    @ApiOperation("搜索保研信息")
    public Result insertBaoyan(@PathVariable("num") int num,
                               @PathVariable("size") int size,BaoyanCondition condition) {
        Page<Baoyan> page = new Page<>(num, size);
        return baoyanService.searchBaoyan(page, condition);
    }

    @GetMapping("/download/baoyan/template")
    @ApiOperation("下载excel模板文件")
    public String downloadExcel(){
        return "http://localhost:8887/baoyan.xlsx";//返回前端一个下载链接
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "Excel导入竞赛信息")
    public Result excelBaoyanInfo(@RequestParam("file") MultipartFile file){
        return this.baoyanService.excelImport(file);
    }
}
